package com.tjetc.controller;

import com.github.pagehelper.PageInfo;
import com.tjetc.domain.Admin;
import com.tjetc.service.AdminService;
import com.tjetc.util.JwtUtils;
import com.tjetc.vo.Result;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (admin)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Admin selectOne(Integer id) {
        return adminService.selectByPrimaryKey(id);
    }

    @PostMapping("login")
    public Result login(@RequestBody Admin admin) {
        return adminService.login(admin);
    }

    @GetMapping("/checkToken")
    public boolean checkToken(@RequestHeader("token") String token) {
        System.out.println("校验token");
        return JwtUtils.checkToken(token);
    }
    //updateAdminDetails
    @GetMapping("updateAdminDetails")
    public Result updateAdminDetails(
            @RequestParam("id") Integer id,
            @RequestParam(value = "username",required = false) String username,
            @RequestParam("name") String name,
            @RequestParam(value = "Opassword",required = false) String Opassword,
            @RequestParam(value = "Npassword",required = false) String Npassword,
            @RequestParam(value = "hid",required = false) Integer hid) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setName(name);
        admin.setUsername(username);
        if (Opassword!=null){
            admin.setPassword(DigestUtils.md5DigestAsHex(Npassword.getBytes()));
        }
        admin.setHid(hid);
        return adminService.updateAdminDetails(admin,Opassword);
    }

    @GetMapping("selAll")
    public PageInfo<Admin> selAll(
            @RequestParam(value = "username", defaultValue = "") String username,
//            @RequestParam(value = "password",defaultValue = "")String password,
            @RequestParam(value = "curPage", defaultValue = "1") Integer curPage,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize

    ) {
        return adminService.selAll(username, curPage, pageSize);
    }
    @GetMapping("getMyDetails")
    public Result getMyDetails(@RequestParam("id") Integer id) {
        return adminService.getMyDetails(id);
    }


    @GetMapping("delById")
    public Integer delById(@RequestParam("id") Integer id) {
        return adminService.delById(id);
    }

    @GetMapping("update")
    public Integer update(
            @RequestParam("id") Integer id, @RequestParam("username") String username, @RequestParam("name") String name, @RequestParam("permission") String permission, @RequestParam(value = "hid",required = false) Integer hid) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setName(name);
        admin.setUsername(username);
        admin.setPermission(permission);
        admin.setHid(hid);
        return adminService.update(admin);
    }

    @GetMapping("add")
    public Integer add(@RequestParam("username") String username, @RequestParam("name") String name, @RequestParam("permission") String permission, @RequestParam(value = "hid",required = false) Integer hid) {
        Admin admin = new Admin();
        admin.setUsername(username);
        System.out.println(name);
        admin.setName(name);
        admin.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        admin.setPermission(permission);
        admin.setHid(hid);
        System.out.println("admin" + admin);
        return adminService.add(admin);
    }
}
